#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    //1.创建管道
    int fd[2] = {0};
    if(pipe(fd) < 0)
    {
        perror("pipe error");
        return 1;
    }
    //2.创建子进程
    pid_t id = fork();
    if(id == 0)
    {
        //关闭释放操作一定要清清楚楚
        //子进程
        close(fd[0]);//关闭读端
        int count = 10;
        const char* msg = "father,this is child,over....";
        while(count--)
        {
            write(fd[1],msg,strlen(msg));
            sleep(1);
        }
        close(fd[1]);
        exit(0);
    }
    //3.父进程关闭读端，子进程被强制关闭（看状态码）
    close(fd[1]);//关闭写端
    close(fd[0]);//强制关闭读端。
    int status;
    waitpid(id,&status,0);
    printf("child get signal:%d\n",status);
    return 0;
}